package com.mrlv.ht.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrlv.ht.pojo.Role;
import com.mrlv.ht.pojo.RoleExample;
import com.mrlv.ht.pojo.RoleModuleExample;
import com.mrlv.ht.pojo.RoleModuleExample.Criteria;
import com.mrlv.ht.pojo.RoleModuleKey;
import com.mrlv.ht.service.IRoleService;

@Controller
public class RoleController {
	
	@Resource
	private IRoleService roleService;
	
	@RequestMapping("/sysadmin/role/list")
	public String list(Model model) {
		List<Role> roles = roleService.selectByExample(new RoleExample());
		model.addAttribute("roles", roles);
		return "/sysadmin/role/jRoleList";
	}
	
	@RequestMapping("/sysadmin/role/tocreate")
	public String tocreate() {
		return "/sysadmin/role/jRoleCreate";
	}
	
	@RequestMapping("/sysadmin/role/insert")
	public String insert(Role role) {
		roleService.insertSelective(role);
		return "redirect:/sysadmin/role/list";
	}
	
	@RequestMapping("/sysadmin/role/toupdate")
	public String toupdate(Model model, String roleId) {
		if(roleId == null) {
			model.addAttribute("err", "请选择一个角色");
			return "forward:/sysadmin/role/list";
		}
		String[] array = roleId.split(",");
		if(array.length > 1) {
			model.addAttribute("err", "只能选择一个角色");
			return "forward:/sysadmin/role/list";
		}
		Role role = roleService.selectByPrimaryKey(array[0]);
		model.addAttribute("role", role);
		return "/sysadmin/role/jRoleUpdate";
	}
	
	@RequestMapping("/sysadmin/role/roleAction_update")
	public String update(Role role) {
		roleService.updateByPrimaryKeySelective(role);
		return "redirect:/sysadmin/role/list";
	}
	
	@RequestMapping("/sysadmin/role/tomodule")
	public String tomodule(Model model, String roleId) {
		if(roleId == null) {
			model.addAttribute("err", "请选择一个角色");
			return "forward:/sysadmin/role/list";
		}
		String[] array = roleId.split(",");
		if(array.length > 1) {
			model.addAttribute("err", "只能选择一个角色");
			return "forward:/sysadmin/role/list";
		}
		String json = roleService.getJson(roleId);
		model.addAttribute("json", json);
		model.addAttribute("roleId", roleId);
		return "/sysadmin/role/jRoleModule";
	}
	
	@RequestMapping("/sysadmin/role/module")
	public String module(String[] moduleIds, String roleId) {
		RoleModuleExample re = new RoleModuleExample();
		Criteria reCriteria = re.createCriteria();
		reCriteria.andRoleIdEqualTo(roleId);
		List<RoleModuleKey> res = roleService.selectByExample(re);
		for(RoleModuleKey rek: res) {
			boolean fig = false;
			for(String moduleId : moduleIds) {
				if(rek.getModuleId().equals(moduleId)) {
					fig = true;
				}
			}
			if(!fig) {
				roleService.deleteByPrimaryKeyByModule(rek.getModuleId());
			}
		}
		
		for(String moduleId : moduleIds) {
			RoleModuleExample rme = new RoleModuleExample();
			Criteria rmeCriteria = rme.createCriteria();
			rmeCriteria.andModuleIdEqualTo(moduleId);
			rmeCriteria.andRoleIdEqualTo(roleId);
			List<RoleModuleKey> rms = roleService.selectByExample(rme);
			if(rms.size() == 0) {
				RoleModuleKey rm = new RoleModuleKey();
				rm.setRoleId(roleId);
				rm.setModuleId(moduleId);
				roleService.insertModuLeRole(rm);
			}
		}
		return "redirect:/sysadmin/role/list";
	}
	
	@RequestMapping("/sysadmin/role/delete")
	public String delete(String roleId) {
		if(roleId == null) {
			return "redirect:/sysadmin/role/list";
		}
		String[] array = roleId.split(",");
		if(array.length == 1) {
			roleService.deleteByPrimaryKey(array[0]);
		}
		else if(array.length > 1) {
			roleService.deleteBatch(array);
		}
		return "redirect:/sysadmin/role/list";
	}
	/*	
	@Test
	public void testJson() {
		ObjectMapper mapper = new ObjectMapper();
		ModuLe module = new ModuLe();
		module.setModuleId("100");
		module.setName("实习");
		module.setParentId("1000");
		try {
			String writeValueAsString = mapper.writeValueAsString(module);
			System.out.println(writeValueAsString);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
}
